<?php
	require('evaluate.doc.php');
	//新增评价
	$app->post(
		'/evaluate/add',
		function() use($app, $db2){
			$data = $app->request()->post();
			$echo['error'] = "";
			if($data && isset($data['reserveid']) && isset($data['memberid']) && isset($data['star']) && isset($data['content']) && isset($data['isanonymous'])){
				$reserve=$db2->select(2,'tb_reserve','*',array('tbid'=>$data['reserveid'],'state'=>6,'member_id'=>$data['memberid']));
				if($reserve==1){
					$set=array(
						'reserve_id' => $data['reserveid'],
						'member_id' => $data['memberid'],
						'star' => $data['star'],
						'content' => urldecode($data['content']),
						'isanonymous' => $data['isanonymous'],
						'dt' => date('Y-m-d H:i:s')
					);
					if(!empty($_FILES['img'])){
						$imgurl = creatfilesimg($_FILES['img'],'',$data['memberid'],1,'evaluate');
						$set['img'] = $imgurl;
					}
					$db2->insert('tb_evaluate', $set);
					$db2->update('tb_reserve',array('state'=>7),array('tbid'=>$data['reserveid']));
				}else{
					$echo['error'] = "预约记录不存在，或不在可评价状态";
				}
			}else{
				$echo['error'] = '数据缺失，请检查';
			}
			echo json_encode($echo);
		}
	);
	//评价列表
	$app->get(
		'/evaluate/list',
		function() use($app, $db2){
			$data = $app->request()->get();
			$echo['error'] = "";
			if($data && isset($data['curriculumid']) && isset($data['from']) && isset($data['to'])){
				$where=array('reserve_id'=>$db2->select(3,'tb_reserve','tbid',array('curriculum_id'=>$db2->select(3,'tb_curriculum_snap','tbid',array('curriculum_id'=>$data['curriculumid'])))));
				$echo['count'] = $db2->select(2, 'tb_evaluate', '*', $where);
				$where['limit'] = array($data['from'], $data['to']);
				$evaluate=$db2->select(0,'tb_evaluate','*,('.$db2->select(4,'tb_member','nickname',array('#tb_member.tbid'=>'tb_evaluate.member_id')).')[nickname]',$where);
				foreach($evaluate as $v){
					$tmp['nickname']=$v['nickname'];
					$tmp['head'] = file_exists('dofiles/member/'.$v['member_id'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$v['member_id'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
					$tmp['star']=$v['star'];
					$tmp['content']=$v['content'];
					$tmp['img']=$v['img'];
					$tmp['isanonymous']=$v['isanonymous'];
					$tmp['dt']=$v['dt'];
					$echo['data'][]=$tmp;
				}
			}else{
				$echo['error'] = '数据缺失，请检查';
			}
			echo json_encode($echo);
		}
	);
	
	//教练评价列表
	$app->get(
	    '/evaluate/coach_list',
	    function() use($app, $db2){
	        $data = $app->request()->get();
	        $echo['error'] = "";
	        if($data && isset($data['coachid']) && isset($data['from']) && isset($data['to'])){
	            //好评数
	            $sql = "select count(*) as sum from tb_evaluate where reserve_id in".
	                " ( select tbid from tb_reserve where curriculum_id in (select tbid from tb_curriculum_snap where coach_id=?) )";
	            $ht = $db2->prepare($sql);
	            $ht->execute(array($data['coachid']));
	            $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
	            
	            $echo['count'] = $tmp[0]['sum'];
	            
	            
	            //教练名
	            $sql = "select nickname from tb_coach where tbid=?";
	            $ht = $db2->prepare($sql);
	            $ht->execute(array($data['coachid']));
	            $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
	            $echo['coach_name'] = $tmp[0]['nickname'];
	            
	            
	            //好评数
	            $sql = "select a.*,b.nickname,b.photo from tb_evaluate as a left join tb_member as b on a.member_id=b.tbid where a.reserve_id in".
	                " ( select tbid from tb_reserve where curriculum_id in (select tbid from tb_curriculum_snap where coach_id=?) ) limit ?,?";
	            $ht = $db2->prepare($sql);
	            $ht->execute(array($data['coachid'], $data['from'], $data['to']));
	            $tmp = $ht->fetchAll(PDO::FETCH_ASSOC);
	            
	            foreach($tmp as $key=>&$value){
	                //教练名
    	            $value['headimg'] = file_exists('dofiles/member/'.$value['member_id'].'/head/head.jpg') ? SITEROOTURL.'dofiles/member/'.$value['member_id'].'/head/head.jpg' : SITEROOTURL.'sqinc/skin/default/hdicon.png';
	                
	            }
	            
	            $echo['data'] = $tmp;
	        }else{
	            $echo['error'] = '数据缺失，请检查';
	        }
	        echo json_encode($echo);
	    }
	);
?>
